import time

from appium.webdriver.common.mobileby import MobileBy


class LoanOperation:
  def loan(self, driver, loan_amount, loan_term, repayment_way, card, pwd):
    # 点击同意按钮
    driver.find_element(MobileBy.ID, "com.duxiaoman.wallet:id/protocol_dialog_pos_btn").click()
    time.sleep(5)
    # 点击拒绝按钮
    driver.find_element(MobileBy.XPATH,
                              "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.Button[1]").click()
    time.sleep(5)

    driver.find_element(MobileBy.ACCESSIBILITY_ID, '借钱').click()
    time.sleep(5)
    # 填写相关信息并提交
    driver.find_element(MobileBy.ID, "com.baidu.wallet:id/et_amount").send_keys(loan_amount)
    driver.find_element(MobileBy.ID, "com.baidu.wallet:id/et_term").send_keys(loan_term)
    driver.find_element(MobileBy.ID, "com.baidu.wallet:id/et_way").send_keys(card)
    driver.find_element(MobileBy.ID, "com.baidu.wallet:id/et_card").send_keys(repayment_way)
    driver.find_element(MobileBy.ID, "com.baidu.wallet:id/et_pwd").send_keys(pwd)
    driver.find_element(MobileBy.ID, "com.baidu.wallet:id/btn_submit").click()
    time.sleep(5)
    # 获取提示信息
    result = driver.find_element(MobileBy.ID, "com.baidu.wallet:id/information").text
    time.sleep(5)
    if result == '借款成功！':
        return 1
    else:
        return 0
